home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 14 / MacFormat n. 14 (Spain) / MacFormat 14.bin / Shareware Internet / Educación / xComputer 1.2.1 / Examples / Example 5 - Input⁄Output < prev    next >
Encoding:
Text File  |  1995-12-29  |  5.3 KB  |  127 lines

  1. ; Example 5: Input/Output
  2.  
  3. ; This example illustrates the use of Input/Output instructions
  4. ; in the xComputer.  The input/output facilities of xComputer
  5. ; are very limited.  Furthermore, unlike all the other aspects
  6. ; of xComputer's machine language, they are "fudged" (rather 
  7. ; than being simulated on the same step-by-step basis that
  8. ; a real CPU might use).
  9.  
  10. ; The input instruction is gtc.  Output instructions are
  11. ; ptc, pti, ptu, and ptb.  Note that each output item
  12. ; appears on a line by itself in the Output list (which
  13. ; appears when I/O Services are turned on).  For more details,
  14. ; look at the end of the file "READ ME (with instructions!)."
  15.  
  16. ; To use this program, you should turn on the "Use I/O Services"
  17. ; option in the Options menu.  Then, load and run the program
  18. ; as usual.  While the program is running, you can type characters.
  19. ; These characters are put in an "input queue".  The program
  20. ; fetches and processes these characters one at a time.
  21. ; At "Fastest" speed, the input is processed just about as
  22. ; fast as you can type it.  At slower speeds, you can follow
  23. ; what is going on in detail.
  24.  
  25. ; The program implements a "calculator" that can do addition
  26. ; and subtraction.  While the program is running, type in
  27. ; numbers separated by + and - signs.  Type = to get the
  28. ; final sum.  Any other input will make the program
  29. ; output a "?" and reset the sum to 0.  In this program,
  30. ; the computer sits in a loop reading typed characters and
  31. ; processing them.  This method of handling input is
  32. ; called "polling".  Another method that uses "interrupts"
  33. ; is illustrated in the next example file.
  34.  
  35. @PC 0   ; Make sure the PC contains 0, the starting location
  36.         ;   of the program.
  37.  
  38. start:  lod-c 0   ; Set up the starting state: sum = num = 0,
  39.         sto num   ;     and op = "+".  ("Op" represents a
  40.         sto sum   ;     pending operation that will be
  41.         lod-c '+  ;     applied to sum and num; since sum
  42.         sto op    ;     is 0, applying "+" will just set
  43.                   ;     set sum := num.)
  44.  
  45. loop:   gtc       ; Get a typed character from the input queue.
  46.         jmz loop  ; If no character is available, 0 is returned;
  47.                   ;    in that case just jump back to "loop"
  48.                   ;    to get the next character.
  49.         sto ch    ; Save the typed character in "ch".
  50.  
  51.         sub-c '+  ; Test the typed character.  If it is
  52.         jmz do_op ;    +, -, or =, then jump to "do_op".
  53.         lod ch    ;    If it is a digit between 0 and 9,
  54.         sub-c '-  ;    jump to "digit".  Otherwise, it is
  55.         jmz do_op ;    illegal input; jump to "bad".
  56.         lod ch
  57.         sub-c '=
  58.         jmz do_op
  59.         lod ch
  60.         sub-c '0
  61.         jmn bad
  62.         lod ch
  63.         sub-c '9
  64.         jmn digit
  65.         jmz digit
  66.  
  67. bad:    lod-c '?    ; Illegal input was typed.  Output the
  68.         ptc         ;     chatacter "?" followed by a space,
  69.         lod-c 32    ;     then return the calculator to its
  70.         ptc         ;     starting state.
  71.         jmp start
  72.  
  73. digit:  lod ch      ; A digit was typed.
  74.         sub-c '0    ; Convert the character to the corresponding
  75.         sto ch      ;     number and put that number back in ch.
  76.         lod num     ; Set num := 10*num + ch, effectively adding
  77.         shl         ;     the typed digit onto the end of num.
  78.         shl         ;     (10*num is computed by shifting num
  79.         shl         ;     left three times, giving 8*num, and
  80.         add num     ;     then adding num twice to that.)
  81.         add num
  82.         add ch
  83.         sto num
  84.         jmp loop    ; After processing the input digit, go
  85.                     ;    back to "loop" to get the next input
  86.                     ;    character.
  87.  
  88. do_op:  lod num     ; A "+", "-", or "=" was typed.  Output
  89.         ptu         ;    the current value of "num", and then
  90.         lod op      ;    either add "num" to "sum" or subtract
  91.         sub-c '+    ;    it from "sum", depending on the
  92.         jmz plus    ;    pending operation, "op".
  93. minus:  lod sum
  94.         sub num
  95.         sto sum
  96.         jmp fin_op
  97. plus:   lod sum
  98.         add num
  99.         sto sum
  100. fin_op: lod-c 0     ; After the calculation, "num" is reset to 0
  101.         sto num     ;    so that the user can start typing the
  102.         lod ch      ;    next number.  "Ch", the operation that
  103.         ptc         ;    was just input, is printed.
  104.         sub-c '=    ; If "ch" is "+" or "-", then it is
  105.         jmz equals  ;    stored in "op" to become the next
  106.         lod ch      ;    pending operation.
  107.         sto op
  108.         jmp loop
  109. equals: lod sum     ; If the typed character was "=", then
  110.         ptu         ;    the final answer, "sum" is output,
  111.         lod-c 32    ;    followed by a space (ASCII 32) to
  112.         ptc         ;    separate computations in the output list.
  113.         jmp start   ; Then the calculator is returned to its
  114.                     ;    starting state to get ready for the
  115.                     ;    next computation.
  116.  
  117. op:     data    ; Pending operation, that is, a "+" or "-"
  118.                 ;    that will be performed after its second
  119.                 ;    operand has been entered.
  120. ch:     data    ; Holds the character typed most recently
  121.                 ;    by the user.
  122. num:    data    ; Holds a number as itis being typed by
  123.                 ;    the user.
  124. sum:    data    ; Holds the result of any computation 
  125.                 ;    performed so far, while the user is
  126.                 ;    typing the next number.
  127.